package cn.thinkluck.pattern.observier.subscriber;

/**
 * 气象站发布者
 *
 * @Author liutao
 * @DateTime 2024/7/24 下午9:15
 */
public class WeatherStation {
    /**
     * 发布者依赖消息总线
     */
    private EventBus eventBus;
    /**
     * 温度
     */
    private float temperature;

    public WeatherStation(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    /**
     * 改变温度时通知观察者
     *
     * @param temperature 温度
     */
    public void changeTemperature(float temperature) {
        this.temperature = temperature;
        eventBus.publishEvent("changeTemperature", temperature);
    }

    /**
     * 发布者与订阅者解耦
     */
    public static void main(String[] args) {
        //订阅事件
        EventBus eventBus = new EventBus();
        eventBus.registerSubscriber("changeTemperature", new WebSubscriber());
        eventBus.registerSubscriber("changeTemperature", new AppSubscriber());
        //新建天气站
        WeatherStation weatherStation = new WeatherStation(eventBus);
        //发布温度变化
        weatherStation.changeTemperature(27);
    }


}
